<template>
  <el-dialog title="新建培训计划" width="800" :before-close="handleClose">
    <el-form :model="form" label-width="auto">
      <el-form-item label="计划名称">
        <el-input v-model="form.title" placeholder="请输入培训计划名称" />
      </el-form-item>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="开始日期">
            <el-date-picker
              v-model="form.startDate"
              type="date"
              placeholder="选择日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束日期">
            <el-date-picker
              v-model="form.endDate"
              type="date"
              placeholder="选择日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="关联课程">
          <el-checkbox-group v-model="selectedCourses">
            <el-checkbox
              v-for="course in courses"
              :key="course.course.id"
              :label="course.course.id"
            >
              {{ course.course.title }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-row>
      <el-form-item label="培训类型">
        <el-select
          v-model="form.category"
          placeholder="请选择培训类型"
          style="width: 100%"
        >
          <el-option label="理论学习" value="0" />
          <el-option label="技能实操" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="计划描述">
        <el-input
          v-model="form.content"
          type="textarea"
          :rows="3"
          placeholder="请输入培训计划详细描述"
        />
      </el-form-item>

      <!--      <el-form-item label="相关课程">-->
      <!--        <el-input-->
      <!--          v-model="form.relatedCourses"-->
      <!--          placeholder="搜索课程"-->
      <!--          style="width: calc(100% - 60px)"-->
      <!--        />-->
      <!--        <el-button type="primary" style="margin-left: 10px">添加</el-button>-->
      <!--        <div class="el-form-item__extra">可添加多个相关课程</div>-->
      <!--      </el-form-item>-->
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="savePlan"> 保存</el-button>
        <el-button type="success" @click="uploadPlan"> 上传</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, computed } from "vue";
import { ElMessage } from "element-plus";
import { modalControllers } from "@/stores/modalController.js";
import { usePlanStores } from "@/stores/training-plan.js";
import { useUserStore } from "@/stores/user.js";

const userStore = useUserStore();
const modalController = modalControllers();
const planStore = usePlanStores();
const form = ref(planStore.planingNow);
const courses = computed(() => {
  return userStore.teacher_myCourse;
});
const selectedCourses = ref([]);
const handleClose = () => {
  modalController.closePopup("newPlanModal");
  Object.assign(form, {
    planTitle: "",
    startDate: "",
    endDate: "",
    planType: "",
    planDescription: "",
    relatedCourses: "",
    participants: "",
    reminderStart: false,
    reminderDeadline: false,
  });
};

const savePlan = () => {
  // Implement your save logic here
  console.log("保存的培训计划:", form);
  ElMessage.success("培训计划已保存！");
};
const uploadPlan = () => {
  console.log("上传的培训计划", form);
  console.log(selectedCourses.value);
  planStore.updatePlan(userStore.user.id, selectedCourses.value);
  handleClose();
};
</script>

<style scoped>
.el-form-item__extra {
  font-size: 12px;
  color: #909399;
  line-height: 1;
  margin-top: 5px;
}
</style>
